package com.yenlien.traceyun.business.service;


import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import com.yenlien.traceyun.business.interceptor.CommonProperties;
import com.yenlien.traceyun.common.utils.FileUtil;
import com.yenlien.traceyun.common.utils.RandomUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.InputStream;
import java.util.List;


/**
 * @author: bigwangy@sina.com
 * @create: 2020-03-12
 * @description:七牛云服务
 **/

@Service
public class QiniuService {

    @Autowired
    private UploadManager uploadManager;

    @Autowired
    private BucketManager bucketManager;

    @Value("${traceyun.app.code}")
    private String appCode;

    @Autowired
    private CommonProperties commonProperties;

    @Autowired
    private Auth auth;

    private Logger logger = LoggerFactory.getLogger(QiniuService.class);

    /**
     * 七牛文件上传
     *
     * @param file
     * @return
     * @throws QiniuException
     */
    public String upload(File file) throws QiniuException {
        String fileName = appCode + "_" + System.currentTimeMillis() + FileUtil.getSuffix(file.getName());
        Response response = uploadManager.put(file, fileName, auth.uploadToken(commonProperties.getQiniuBucket(), fileName));
        if (response.isOK()) {
            return fileName;
        }
        throw new QiniuException(response);
    }


    /**
     * 七牛文件上传
     *
     * @param inputStream
     * @return
     * @throws QiniuException
     */
    public String upload(InputStream inputStream, String fName) throws QiniuException {
        String fileName = appCode + "_" + System.currentTimeMillis() + FileUtil.getSuffix(fName);
        Response response = uploadManager.put(inputStream, fileName, auth.uploadToken(commonProperties.getQiniuBucket(), fileName), null, null);
        if (response.isOK()) {
            return fileName;
        }
        throw new QiniuException(response);
    }

    /**
     * 七牛文件下载
     *
     * @param fileName
     * @return
     */
    public String download(String fileName) {

        String baseUrl = commonProperties.getQiniuPath() + "/" + fileName;
        return auth.privateDownloadUrl(baseUrl);
    }

    /**
     * 七牛文件删除
     *
     * @param fileName
     * @return
     */
    public boolean delete(String fileName) {
        try {
            Response response = bucketManager.delete(commonProperties.getQiniuBucket(), fileName);
            if (response.isOK()) {
                return true;
            } else {
                logger.error("delete qiniu file:{}  error :{}", fileName, response.bodyString());
            }
        } catch (Exception e) {
            logger.error("delete qiniu file:{}  error :{}", fileName, e.getMessage());
        }
        return false;
    }

}
